-- UILearnNewSkillEffect
-- Created by zouyb Jan/5/2015
-- 获得新称号特效界面

-- UILearnNewSkillEffect继承自Layer
UILearnNewSkillEffect = class("UILearnNewSkillEffect", function()
    return cc.Layer:create();
end);

function UILearnNewSkillEffect.create(classId, bonusList)
    return UILearnNewSkillEffect.new(classId, bonusList);
end

-- 构造函数
function UILearnNewSkillEffect:ctor(classId, bonusList)
    -- 初始化
    self:setName("UILearnNewSkillEffect");
    local node = cc.CSLoader:createNode("layout/hero/LearnNewSkillEffect.csb");
    self:addChild(node);
    self.node = node;
    self.classId = classId;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    local info = CareerM.getCareerInfo(classId);

    -- 设置标题
    local titleNode = findChildByName(self.node, "CT/title");
    titleNode:setString(info.name);
    titleNode:setVisible(false);
    local kerning = 0;
    if not isFullWidthLang() then
        kerning = -3;
    end
    TextStyleM.setTextStyle(titleNode, TextStyleM.TEXT_SIZE_LARGE2, TextStyleM.TEXT_COLOR_BRIGHT_YELLOW, true, kerning);

    local iconNode = findChildByName(self.node, "CT/icon");
    local iconPath = getCareerIconPath(info.icon);
    iconNode:loadTexture(iconPath);
    iconNode:setVisible(false);

    -- 注册任意地方点击返回事件
    local bgImage = findChildByName(self.node, "bg/bgImg");
    local function onBgBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UILearnNewSkillEffect");

            local function done()
                EventMgr.fire(event.JOIN_CAREER_ANIM_DONE);
            end

            if bonusList ~= nil and #bonusList > 0 then
                showBonusNotifyForm(getLocStr("new_career_bonus"), bonusList, nil, nil, nil, nil, nil, done);
            else
                done();
            end
        end
    end
    bgImage:addTouchEventListener(onBgBackClick);

    -- 适配
    self:resize();

    self:playEffect();

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(ev)
        if ev == "enter" then
            --AudioM.playFx("title_gain");
        end
    end);
end

-- 特效
function UILearnNewSkillEffect:playEffect()
    local CT = findChildByName(self.node, "CT/tip");
    local tipNode = findChildByName(self.node, "CT/tip");
    local titleNode = findChildByName(self.node, "CT/title");
    local iconNode = findChildByName(self.node, "CT/icon");
    local iconEffectNode = findChildByName(self.node, "CT/effect_node");
    -- 图标特效
    local function showIconEffect()
        iconNode:setVisible(true);
        iconNode:setScale(1.8);

        iconEffectNode:setVisible(true);
        iconEffectNode:setScale(1.8);

        local scaleTo = cc.EaseIn:create(cc.ScaleTo:create(0.5, 1.5), 3.5);
        local callFunc = cc.CallFunc:create(function()
            playEffect(iconEffectNode, 1128, 65, 90);
        end);

        iconNode:runAction(cc.Sequence:create(scaleTo, callFunc));
    end

    -- 称号特效
    local function showTitleEffect()
        titleNode:setVisible(true);

        local interval = 1;

        if getLang() ~= "zh" and getLang() ~= "tw" then
            interval = 2;
        end

        setTextRotate(string.getWCharCount(titleNode:getString()), titleNode, self, -360, interval);

        local oldPosX = titleNode:getPositionX();
        local oldPosY = titleNode:getPositionY();

        titleNode:setPositionX(-DESIGN_WIDTH);
        local moveTo = cc.MoveTo:create(0.5, cc.p(oldPosX, oldPosY));

        local callFunc = cc.CallFunc:create(function()
            playEffect(titleNode, 1127, 100, 20);

            local function delay()
                showIconEffect();
            end
            performWithDelay(self, delay, 0.8);
        end);

        titleNode:runAction(cc.Sequence:create(moveTo, callFunc));
    end

    -- 标题特效
    local function playTipEffect()
        local str = getLocStr("gain_new_career");

        local fontSize = 80 * getLocFontSizeMulti();
        local bgPath = "images/ui/text_mask/text_mask.png";

        tipNode:setString(str);
        -- 生成艺术字并旋转飞入
        genArtWordAndRotateFlyIn(tipNode, fontSize, bgPath);

        local function next()
            AudioM.playFx("title_gain");
            showTitleEffect();
        end
        performWithDelay(tipNode, next, 0.8);
    end

    playTipEffect();
end

-- 适配
function UILearnNewSkillEffect:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");
end





